/**create by liuhua at 2019年8月9日 上午9:32:28**/
package com.star.truffle.module.fzjz.controller.api;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.annotation.IgnoreSecurity;
import com.star.truffle.core.eureka.StarGateway;
import com.star.truffle.core.web.ApiCode;
import com.star.truffle.core.web.ApiResult;
import com.star.truffle.module.fzjz.dto.req.HourDataByTimeAndAreasRequestDto;
import com.star.truffle.module.fzjz.dto.req.MinuteDataByTimeAndStationsRequestDto;
import com.star.truffle.module.fzjz.dto.res.cimiss.HourDataResponseDto;
import com.star.truffle.module.fzjz.dto.res.cimiss.MinuteItemResponseDto;
import com.star.truffle.module.fzjz.service.CimissApiService;
import com.star.truffle.module.fzjz.service.CimissWarnApiService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/cimiss")
@Api(tags = "CimissApiController")
public class CimissApiController {

  @Autowired
  private CimissApiService cimissApiService;
  @Autowired
  private CimissWarnApiService cimissWarnApiService;
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/data/this/hour", method = RequestMethod.POST)
  @ApiOperation(value = "获取郴州最近一小时的雨量", notes = "获取郴州最近一小时的雨量", httpMethod = "POST")
  public ApiResult<Map<String, List<MinuteItemResponseDto>>> queryOneHourData(Double red, Double blue) {
    try {
      Map<String, List<MinuteItemResponseDto>> items = this.cimissWarnApiService.queryOneHourData(red, blue);
      return ApiResult.success(items);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/data/areas/for/hour", method = RequestMethod.POST)
  @ApiOperation(value = "根据地区编码获取时间段内的每小时降水量", notes = "根据地区编码获取时间段内的每小时降水量", httpMethod = "POST")
  public ApiResult<HourDataResponseDto> getHourDataByAreas(@Valid @RequestBody HourDataByTimeAndAreasRequestDto hourDataByTimeAndAreasRequestDto) {
    try {
      HourDataResponseDto items = this.cimissApiService.getHourDataByAreas(hourDataByTimeAndAreasRequestDto.getBeginTime(), hourDataByTimeAndAreasRequestDto.getEndTime(), hourDataByTimeAndAreasRequestDto.getAreaIds(), null, null);
      return ApiResult.success(items);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/data/areas/for/minute", method = RequestMethod.POST)
  @ApiOperation(value = "根据地区编码获取时间段内的x分钟降水量", notes = "根据地区编码获取时间段内的x分钟降水量", httpMethod = "POST")
  public ApiResult<List<MinuteItemResponseDto>> getMinuteDataByAreas(@Valid @RequestBody HourDataByTimeAndAreasRequestDto hourDataByTimeAndAreasRequestDto) {
    try {
      List<MinuteItemResponseDto> items = this.cimissApiService.getMinuteDataByAreas(hourDataByTimeAndAreasRequestDto);
      return ApiResult.success(items);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
  
  @IgnoreSecurity
  @StarGateway
  @RequestMapping(value = "/data/stations/for/minute", method = RequestMethod.POST)
  @ApiOperation(value = "根据区站编号id获取时间段内每分钟的降水量", notes = "根据区站编号id获取时间段内每分钟的降水量", httpMethod = "POST")
  public ApiResult<List<MinuteItemResponseDto>> getMinuteDataByStations(@Valid @RequestBody MinuteDataByTimeAndStationsRequestDto minuteDataByTimeAndStationsRequestDto) {
    try {
      List<MinuteItemResponseDto> items = this.cimissApiService.getMinuteDataByStations(minuteDataByTimeAndStationsRequestDto.getBeginTime(), minuteDataByTimeAndStationsRequestDto.getEndTime(), minuteDataByTimeAndStationsRequestDto.getStationIds());
      return ApiResult.success(items);
    } catch (StarServiceException e) {
      return ApiResult.fail(e.getCode(), e.getMsg());
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return ApiResult.fail(ApiCode.SYSTEM_ERROR);
    }
  }
}
